﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient; 
//PS0039
using System.Threading;

public partial class PublicSite_ActivateAccount : System.Web.UI.Page
{
    int userID = 0, Result = 0;
    RegistrationData _objRegistrationData = new RegistrationData();
    BusinessLogic busin = new BusinessLogic();
  
    protected void Page_Load(object sender, EventArgs e)
    { 

        if (!IsPostBack)
        {
            checkLinkExpired();
        }
    }
    protected void imgbtnActivate_Click(object sender, ImageClickEventArgs e)
    {
        if (Request.QueryString["id"] != null)
        { 
            string strCode = Request.QueryString["id"].ToString();
            int length = strCode.Length;
            string strUserID = strCode.Substring(strCode.LastIndexOf("-") + 1);
            userID = Convert.ToInt32(strUserID);
            _objRegistrationData.UserID = userID;

            Result = _objRegistrationData.UpdateAccount();
            if (Result > 0)
            {
                lblMsg.Text = "Your account has been activated."; 
                Thread.Sleep(10);
                DataSet ds = new DataSet();
                _objRegistrationData.UserID = userID;
                ds = _objRegistrationData.GetUserID(userID);
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        busin.InsertSession(ds, false);   
                    }
                }
                Response.Redirect(Common.LoggedPage);
            }
        }
    }

    protected void checkLinkExpired()
    {
        if (Request.QueryString["id"] != null)
        {
            string strCode = Request.QueryString["id"].ToString();
            string strUserID = strCode.Substring(strCode.LastIndexOf("-") + 1);
            userID = Convert.ToInt32(strUserID);
            _objRegistrationData.UserID = userID;
            SqlDataReader rd = _objRegistrationData.checkIFAccountUpdated();
            if (rd.Read())
            {
                if (rd["ActivatedbyEmail"] != DBNull.Value)
                {
                    if (Convert.ToBoolean(rd["ActivatedbyEmail"]))
                    {
                        tblActivate.Visible = false;
                        pActivate.Visible = false;
                        lblMsg.Text = "Your link has been expired.";
                    }
                }
            }
            else
            {
                pActivate.Visible = true;
                tblActivate.Visible = true;
            }
        }
    }

}
